/**
 * @license
 * Copyright 2020 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
'use strict';

/**
 * @fileoverview Image fields test blocks.
 * @author samelh@google.com (Sam El-Husseini)
 */

import * as Blockly from 'blockly/core';

Blockly.defineBlocksWithJsonArray([
  {
    type: 'test_images_datauri',
    message0: 'Image data: URI %1',
    args0: [
      {
        type: 'field_image',
        src: '',
        width: 50,
        height: 50,
        alt: '*',
      },
    ],
    style: 'text_blocks',
  },
  {
    type: 'test_images_small',
    message0: 'Image too small %1',
    args0: [
      {
        type: 'field_image',
        src: 'media/30px.png',
        width: 50,
        height: 50,
        alt: '*',
      },
    ],
    style: 'text_blocks',
  },
  {
    type: 'test_images_large',
    message0: 'Image too large %1',
    args0: [
      {
        type: 'field_image',
        src: 'media/200px.png',
        width: 50,
        height: 50,
        alt: '*',
      },
    ],
    style: 'text_blocks',
  },
  {
    type: 'test_images_fliprtl',
    message0: 'Image flipped RTL %1',
    args0: [
      {
        type: 'field_image',
        src: 'media/arrow.png',
        width: 50,
        height: 50,
        alt: '*',
        flipRtl: true,
      },
    ],
    colour: 160,
  },
  {
    type: 'test_images_missing',
    message0: 'Image missing %1',
    args0: [
      {
        type: 'field_image',
        src: 'missing.png',
        width: 50,
        height: 50,
        alt: '*',
      },
    ],
    style: 'text_blocks',
  },
  {
    type: 'test_images_many_icons',
    message0:
      'Lots of network icons: %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11' +
      ' %12 %13 %14 %15 %16 %17 %18',
    args0: [
      {
        type: 'input_dummy',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/a.png',
        width: 32,
        height: 32,
        alt: 'A',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/b.png',
        width: 32,
        height: 32,
        alt: 'B',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/c.png',
        width: 32,
        height: 32,
        alt: 'C',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/d.png',
        width: 32,
        height: 32,
        alt: 'D',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/e.png',
        width: 32,
        height: 32,
        alt: 'E',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/f.png',
        width: 32,
        height: 32,
        alt: 'F',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/g.png',
        width: 32,
        height: 32,
        alt: 'G',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/h.png',
        width: 32,
        height: 32,
        alt: 'H',
      },
      {
        type: 'input_dummy',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/a.png',
        width: 32,
        height: 32,
        alt: 'A',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/b.png',
        width: 32,
        height: 32,
        alt: 'B',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/c.png',
        width: 32,
        height: 32,
        alt: 'C',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/d.png',
        width: 32,
        height: 32,
        alt: 'D',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/e.png',
        width: 32,
        height: 32,
        alt: 'E',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/f.png',
        width: 32,
        height: 32,
        alt: 'F',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/g.png',
        width: 32,
        height: 32,
        alt: 'G',
      },
      {
        type: 'field_image',
        src: 'https://blockly-demo.appspot.com/static/tests/media/h.png',
        width: 32,
        height: 32,
        alt: 'H',
      },
    ],
    style: 'text_blocks',
  },
]);

Blockly.Blocks['test_images_clickhandler'] = {
  init: function () {
    this.appendDummyInput()
      .appendField('Image click handler')
      .appendField(
        new Blockly.FieldImage(
          'https://blockly-demo.appspot.com/static/tests/media/a.png',
          32,
          32,
          'image with click handler',
          this.onClick_,
        ),
        'IMAGE',
      );
    this.setStyle('text_blocks');
  },
  onClick_: function () {
    alert('Image clicked');
  },
};

/**
 * The Image field category.
 */
export const category = {
  kind: 'CATEGORY',
  name: 'Images',
  contents: [
    {
      kind: 'BLOCK',
      type: 'test_images_datauri',
    },
    {
      kind: 'BLOCK',
      type: 'test_images_small',
    },
    {
      kind: 'BLOCK',
      type: 'test_images_large',
    },
    {
      kind: 'BLOCK',
      type: 'test_images_fliprtl',
    },
    {
      kind: 'BLOCK',
      type: 'test_images_missing',
    },
    {
      kind: 'BLOCK',
      type: 'test_images_many_icons',
    },
    {
      kind: 'BLOCK',
      type: 'test_images_clickhandler',
    },
  ],
};

/**
 * Initialize this toolbox category.
 * @param {!Blockly.WorkspaceSvg} workspace The Blockly workspace.
 */
export function onInit(workspace) {
  // NOP
}
